<cfparam name="attributes.directory" type="string">
<cfparam name="attributes.indexdirectory" type="string">
<cfparam name="attributes.filter" type="string" default="">
<cfparam name="attributes.recurse" type="boolean" default="false">

<!--- 
This default root is used in cases where indexdirectory is a simple value, not
a full path.
--->
<cfset defaultroot = "#server.coldfusion.rootdir#/luceneindex">

<cfif not directoryExists(attributes.directory)>
	<cfthrow message="Directory [#attributes.directory#] does not exist.">
</cfif>

<cfif not find("\", attributes.indexdirectory) and not find("/", attributes.indexdirectory)>
	<cfset attributes.indexdirectory = defaultroot & "/" & attributes.indexdirectory>
</cfif>

<!--- create indexdirectory if it doesn't exist --->
<cfif not directoryExists(attributes.indexdirectory)>
	<cfdirectory action="create" directory="#attributes.indexdirectory#">
</cfif>

<!---
 we support multiple filters, cfdirectory does not. so our logic is so:
 if the listlen of my filter gt 2, we do QoQ. But if it is one
 we don't need to. correction - we still filter by files
--->
<cfif listLen(attributes.filter) is 1 or attributes.filter is "">
	<cfdirectory action="list" directory="#attributes.directory#" recurse="#attributes.recurse#"
			 filter="#listFirst(attributes.filter)#" name="files">

	<!--- filter to files --->
	<cfquery name="files" dbtype="query">
	select	*
	from	files
	where	type = 'File'
	</cfquery>
<cfelse>

	<cfdirectory action="list" directory="#attributes.directory#" recurse="#attributes.recurse#"
			     name="files">

	<cfquery name="files" dbtype="query">
	select	*
	from	files
	where	1=1
	and
	(<cfloop index="f" list="#attributes.filter#">
		name like '%#replace(f,"*","")#'
		<cfif f is not listLast(attributes.filter)>
			or
		</cfif>
	</cfloop>)
	</cfquery>

</cfif>

<!--- If we don't have anything, just leave. --->
<cfif files.recordCount is 0>
	<cfexit method="exitTag">
</cfif>

<!--- Make my stuff --->
<cfset indexWriter = createObject("java", "org.apache.lucene.index.IndexWriter")>
<cfset analyzer = createObject("java", "org.apache.lucene.analysis.standard.StandardAnalyzer").init()>

<!--- CFC for file read --->
<cfset reader = createObject("component", "filereader").init()>

<!--- third arg nukes old data --->
<cfset indexWriter.init(attributes.indexdirectory, analyzer, true)>

<cfset fs = createObject("java", "org.apache.lucene.document.Field$Store")>
<cfset fi = createObject("java", "org.apache.lucene.document.Field$Index")>

<cfloop query="files">
    <cfset fullfile = directory & "/" & name>
    <cfset doc = createObject("java", "org.apache.lucene.document.Document")>

    <!--- add fields --->
    <cfset field = createObject("java", "org.apache.lucene.document.Field")>
    <cfset field.init("path", fullfile, fs.YES, fi.UN_TOKENIZED)>
    <cfset doc.add(field)>

    <cfset field = createObject("java", "org.apache.lucene.document.Field")>
    <cfset field.init("size", javaCast("string",size), fs.YES, fi.UN_TOKENIZED)>
    <cfset doc.add(field)>

    <cfset field = createObject("java", "org.apache.lucene.document.Field")>
	<cfset field.init("modified", datelastmodified, fs.YES, fi.UN_TOKENIZED)>
    <cfset doc.add(field)>

	<!--- get file contents --->
	<cfset contents = reader.read(fullfile)>
    <cfset field = createObject("java", "org.apache.lucene.document.Field")>
	<cfset field.init("contents", contents, fs.NO, fi.TOKENIZED)>

    <cfset doc.add(field)>
	<cfset indexWriter.addDocument(doc)>
</cfloop> 
			 
<cfset indexWriter.close()>			